package com.cskaoyan.controller.topic.admin;


import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.ListData;
import com.cskaoyan.bean.Topic;
import com.cskaoyan.bean.topic.CreateTopicBO;
import com.cskaoyan.bean.topic.DeleteTopicBO;
import com.cskaoyan.bean.topic.UpdateTopicBO;
import com.cskaoyan.service.topic.admin.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin/topic")
public class TopicController {

    @Autowired
    TopicService topicService;

    /**
     *  404 :@RestController
     * @param page
     * @param limit
     * @param title
     * @param subtitle
     * @param sort
     * @param order
     * @return
     */
    @RequestMapping("/list")
    public BaseRespVo list(String page,String limit ,String title,String subtitle,String sort,String order){
        ListData<Topic> list = topicService.list(page, limit, title, subtitle, sort, order);
        if (list != null){
            return BaseRespVo.ok(list,"成功");
        }else {
            return BaseRespVo.fail("failed,try again");
        }
    }

    /**
     * JSON parse error: 删掉DeleteTopicBO 的 goods字段
     * @param deleteTopicBO
     * @return
     */
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody  DeleteTopicBO deleteTopicBO){
        boolean delete = topicService.delete(deleteTopicBO.getId());
        if (delete){
            return BaseRespVo.ok();
        }else {
            return BaseRespVo.fail("failed,try again ");
        }
    }

    /**
     * TODO 专题需要typehandler
     * 此方法创建专题后，要把刚刚创建的专题返回给前端
     * @param createTopicBO
     * @return
     */
    @RequestMapping("create")
    @Transactional
    public BaseRespVo create(@RequestBody CreateTopicBO createTopicBO) {
        boolean b;
        try {
            b = topicService.create(createTopicBO);
        } catch (NullPointerException e) {
            return BaseRespVo.fail("不能有空字段！请检查");
        }

        if (b) {
            int i = topicService.selectMaxId();
            Topic topic = topicService.selectByPrimaryKey(i);
            return BaseRespVo.ok(topic, "成功");
        } else {
            return BaseRespVo.fail("failed ,try again ");
        }
    }

    /**
     * 此方法也需要把刚刚修改的topic传给前端
     * @param updateTopicBO
     * @return
     */
    @RequestMapping("update")
    @Transactional
    public BaseRespVo update(@RequestBody UpdateTopicBO updateTopicBO){
        System.out.println("updateTopicBO = " + updateTopicBO);
        boolean update = topicService.update(updateTopicBO);
        if (update){
            int id = updateTopicBO.getId();
            Topic topic = topicService.selectByPrimaryKey(id);
            return BaseRespVo.ok(topic,"成功");
        }else {
            return BaseRespVo.fail("failed,try again ");
        }
    }





}
